﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class student_write : System.Web.UI.Page
{
    protected Student stu;
    protected void Page_Load(object sender, EventArgs e)
    {
        stu = Student.Check();
        if (stu == null)
        {
            Session.Add(Params.SystemError, "请先登录");
            Response.Redirect(Params.RootHttp + "default.aspx");
        }
        if (!Page.IsPostBack)
        {
            this.MyDataBind();
        }
    }


    public void MyDataBind()
    {
        this.Title = this.stu.RealName + "-写信件";
        this.conNav.Title = "写信件";
        this.conRecvers.UserGuid = this.stu.Guid;
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        string t = this.tbTitle.Text.Trim();
        string b = this.tbBody.Text;
        string gs = this.conRecvers.RecversStr.Trim();
        if (t == "" || gs == "")
        {
            this.info.SetMessage("收信人和标题不能为空！");
            return;
        }
        string fn = "";
        if (this.fuAttachment.PostedFile.FileName != "")
        {
            string ext = System.IO.Path.GetExtension(this.fuAttachment.PostedFile.FileName).Trim().ToLower();
            if (!(ext == ".pdf" || ext == ".doc" || ext == ".docx" || ext == ".txt" || ext == ".rar"))
            {
                this.info.SetMessage("附件上传格式必须为pdf、doc、docx、txt、rar格式");
                return;
            }
            if (this.fuAttachment.PostedFile.ContentLength > Params.MaxFileSize)
            {
                this.info.SetMessage("附件大小请限制在" + Params.MaxFileSize.ToString() + "字节以内");
                return;
            }
            fn = Fun.GetFileName() + ext;
            string fp = Params.FilesFolderPath + fn;
            this.fuAttachment.PostedFile.SaveAs(fp);
        }
        if (gs != "")
        {
            Message.Send(this.stu.Guid, gs.Replace(" ", ","), t, b,fn);
            Fun.Redirect("sent.aspx?uid=" + Request.Params["uid"], "发送成功");
        }
        else
        {
            this.info.SetMessage("发送失败，收件人填写错误");
        }
    }

    protected void tbBody_TextChanged(object sender, EventArgs e)
    {

    }
}
